package com.cti.ftpservice.ws.interceptor.in;

import java.util.List;

import javax.annotation.Resource;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.springframework.stereotype.Component;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;


@Component("authorInInterceptor")
public class AuthorInInterceptor extends AbstractPhaseInterceptor<SoapMessage> {


	
	public AuthorInInterceptor(){
		super(Phase.PRE_INVOKE);
	}
	
	public AuthorInInterceptor(String phase) {
		super(phase);
	}

	@Override
	public void handleMessage(SoapMessage message) throws Fault {
		List<Header> headers=message.getHeaders();
		if(headers==null||headers.size()==0){
			throw new Fault(new IllegalArgumentException("未登录的用户"));
		}
		Header firstHeader=headers.get(0);
		Element element=(Element)firstHeader.getObject();
		NodeList refererNodeList=element.getElementsByTagName("Referer");
		if(refererNodeList.getLength()>0){
			String referer=refererNodeList.item(0).getFirstChild().getNodeValue();
			if(referer!=null&&!"".equals(referer)){
				if(referer.toLowerCase().contains("ctibook")){
					return;
				}
			}
		}

	}

}
